package com.juicygames.shmup.player;

import com.juicygames.framework.GameMath;
import com.juicygames.framework.gamepart.GameComponent;
import com.juicygames.framework.property.space.Position;
import com.juicygames.framework.property.space.Velocity;
import com.juicygames.framework.type.Point;

public class Mover extends GameComponent {
	private static final String LOGTAG = Mover.class.getName();
	
	Position entityPosition;
	Velocity entityVelocity;
	
	public void load() {
		entityPosition = parent.getPart(Position.class);
		entityVelocity = parent.getPart(Velocity.class);
	}
	
	public void setVelTowardPoint(Point otherPoint, int speed) {
		
		Point pointDifference = new Point();
		pointDifference.x = otherPoint.x - entityPosition.get().x;
		pointDifference.y = otherPoint.y - entityPosition.get().y;
		
		double distance = GameMath.distance(otherPoint, entityPosition.get());
		
		if (distance > 5) {
		
			double pointDiffModifier = (double)speed / distance;
			
			entityVelocity.set(pointDifference.multiply(pointDiffModifier));
			
		} else {
			
			entityVelocity.stop();
			
		}
		
//		Log.d(LOGTAG,"Setting new entityVelocity to " + entityVelocity);
		
	}
	
	public void stop() {
		entityVelocity.stop();
	}

}
